<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { equipmentListService, addEquipmentService, updateEquipmentService, deleteEquipmentService } from '@/api/equipment.js'

//课程数据模型
const equipment = ref([{
    "id": 1,
    "name": "跑步机1",
    "location": "自由健身区",
    "status": "1",
    "message": "正常使用中"
}])
//分页条数据模型
const page = ref(1)//当前页
const total = ref(1)//总条数
const pageSize = ref(10)//每页条数
//调用后台接口，完成课程列表查询
const equipmentList = async () => {
    let params = {
        page: page.value,
        pageSize: pageSize.value,
    }
    let result = await equipmentListService(params);
    //渲染视图
    total.value = result.data.total
    equipment.value = result.data.records
}
equipmentList();

//当每页条数发生了变化，调用此函数
const handleSizeChange = (size) => {
    pageSize.value = size
    equipmentList();
}
//当前页码发生变化，调用此函数
const handleCurrentChange = (num) => {
    page.value = num
    equipmentList();
}

//添加、修改课程表单模型
const equipmentForm = ref({
    name: '',
    location: '',
    status: '',
    message: ''
})
//添加、修改会员的表单校验模型
const Rules = ref({
    name: [
        { required: true, message: '请输入课程名称', trigger: 'blur' }
    ],
    location: [
        { required: true, message: '请输入器械位置', trigger: 'blur' }
    ],
    status: [
        { required: true, message: '请选择器械状态', trigger: 'blur' }
    ],
    message: [
        {  message: '请输入器械备注信息', trigger: 'blur' }
    ],
})
//默认不显示抽屉
const visibleDrawer = ref(false)
//为抽屉添加响应数据
const addEquipmentRef = ref()
//后台调用添加课程接口
const onAdd = async () => {
    //点击添加按钮后再进行二次校验
    await addEquipmentRef.value?.validate().catch((err) => { ElMessage.error("校验失败"); return Promise.reject(err) })
    let result = await addEquipmentService(equipmentForm.value);
    ElMessage.success(result.msg ? result.msg : '添加成功')
    //让抽屉消失
    visibleDrawer.value = false
    //刷新列表
    equipmentList();
}
//刷新抽屉表单
const clearEquipmentForm = () => {
    equipmentForm.value = {
        name: '',
        location: '',
        status: '',
        message: ''
    }
}
//取消添加
const cancelDrawer = () => {
    visibleDrawer.value = false
}

//默认不显示对话框
const visibleDialog = ref(false)
//展示编辑对话框弹窗
const showDialog = (row) => {
    clearEquipmentForm()
    visibleDialog.value = true
    //给编辑框回写原先信息
    equipmentForm.value.name = row.name
    equipmentForm.value.location = row.location
    equipmentForm.value.status = row.status
    equipmentForm.value.message = row.message
    //扩展模型添加id
    equipmentForm.value.id = row.id

}
//为对话框添加响应数据
const updateEquipmentRef = ref()
//后台调用修改会员接口
const onUpdate = async () => {
    //点击修改后再进行二次校验
    await updateEquipmentRef.value?.validate().catch((err) => { ElMessage.error("校验失败"); return Promise.reject(err) })
    let result = await updateEquipmentService(equipmentForm.value);
    ElMessage.success(result.msg ? result.msg : '修改成功')
    //让对话框消失
    visibleDialog.value = false
    //刷新列表
    equipmentList();
}

//取消修改
const cancelDialog = () => {
    visibleDialog.value = false
}

const deleteEquipment = (row) => {
    ElMessageBox.confirm(
        '确定要删除吗？',
        '提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            let result = await deleteEquipmentService(row.id);
            ElMessage({
                type: 'success',
                message: result.msg ? result.msg : '删除成功',
            })
            equipmentList();
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消操作',
            })
        })
}
</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>器械管理</span>
                <div class="extra">
                    <el-button type="primary" @click="visibleDrawer = true; clearEquipmentForm()">添加器械</el-button>
                </div>
            </div>
        </template>
        <!-- 会员列表 -->
        <el-table :data="equipment" style="width: 100%">
            <el-table-column label="序号" width="100">
                <template v-slot="scope">
                    <!-- 序号实现自动增加且连续 -->
                    {{ (page - 1) * pageSize + scope.$index + 1 }}
                </template>
            </el-table-column>
            <el-table-column label="器械名称" prop="name" width="200"></el-table-column>
            <el-table-column label="器械位置" prop="location" width="200"></el-table-column>
            <el-table-column label="器械状态" prop="status" width="250">
                <template v-slot="scope">
                    <span v-if="scope.row.status === 0">损坏</span>
                    <span v-if="scope.row.status === 1">正常</span>
                    <span v-if="scope.row.status === 2">维修中</span>
                </template>
            </el-table-column>
            <el-table-column label="器械备注信息" prop="message" width="250"></el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteEquipment(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <el-pagination v-model:current-page="page" v-model:page-size="pageSize" background
            layout="sizes,prev, pager,next,jumper,total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" :total="total">
        </el-pagination>

        <!-- 抽屉中嵌套添加表单 -->
        <el-drawer v-model="visibleDrawer" title="添加器械" direction="rtl">
            <div>
                <el-form ref="addEquipmentRef" :model="equipmentForm" :rules="Rules">
                    <el-form-item label="器械名称" prop="name">
                        <el-input v-model="equipmentForm.name" autocomplete="off" />
                    </el-form-item>
                    <el-form-item label="器械位置" prop="location">
                        <el-input v-model="equipmentForm.location" autocomplete="off" />
                    </el-form-item>
                    <el-form-item label="器械状态" prop="status">
                        <el-select placeholder="请选择" v-model="equipmentForm.status">
                            <el-option label="正常" value="1"></el-option>
                            <el-option label="损坏" value="0"></el-option>
                            <el-option label="维修中" value="2"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="器械备注信息" prop="message">
                        <el-input  v-model="equipmentForm.message" autocomplete="off" />
                    </el-form-item>
                </el-form>
                <!-- 按钮设置 -->
                <div>
                    <el-button @click="cancelDrawer(); clearEquipmentForm()">取消</el-button>
                    <el-button type="primary" @click="onAdd">
                        添加
                    </el-button>
                </div>
            </div>
        </el-drawer>

        <!-- 对话框嵌套编辑表单 -->
        <el-dialog v-model="visibleDialog" title="修改器械">
            <div>
                <el-form ref="updateEquipmentRef" :model="equipmentForm" :rules="Rules">
                    <el-form-item label="器械名称" prop="name">
                        <el-input v-model="equipmentForm.name" autocomplete="off" />
                    </el-form-item>
                    <el-form-item label="器械位置" prop="location">
                        <el-input v-model="equipmentForm.location" autocomplete="off" />
                    </el-form-item>
                    <el-form-item label="器械状态" prop="status">
                        <el-select v-model="equipmentForm.status">
                            <el-option label="正常" value="1"></el-option>
                            <el-option label="损坏" value="0"></el-option>
                            <el-option label="维修中" value="2"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="器械备注信息" prop="message">
                        <el-input  v-model="equipmentForm.message" autocomplete="off" />
                    </el-form-item>

                </el-form>
                <!-- 按钮设置 -->
                <div>
                    <el-button @click="cancelDialog(); clearEquipmentForm()">取消</el-button>
                    <el-button type="primary" @click="onUpdate">
                        修改
                    </el-button>
                </div>
            </div>
        </el-dialog>
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>